import json
import re
import time

import redis
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains

r = redis.Redis(host='localhost', port=6379,db=0)


headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"}

def get_urls(times=None):
    driver = webdriver.Chrome()
    driver.get('https://h.bilibili.com/eden/draw_area#/all/hot')
    js = "var q=document.documentElement.scrollTop=100000"
    while times > 0:

        driver.execute_script(js)  # 将页面拖到最底部 避免翻页元素不可见
        time.sleep(2)
        times -= 1

    urls = driver.find_elements_by_xpath(".//ul[@class='content']/li/dl/a")

    for url in urls:
        url = url.get_attribute('href')
        #redis中没有才保存到redis
        if bytes(url, encoding='utf-8') not in r.smembers("first_urls"):
            r.sadd("first_urls", url)

            yield url
        else:
            print(f"{url}  dupe")


        #     yield url



def get_img(times):
    """

    :param times: 滑动次数
    :return:
    """
    #要保持的目录 要提前创建
    file_direct = './wall/'

    for a in get_urls(times):
        print(f"{a}正在爬取")
        data_id = re.search(r'(https://\w+.*?/)(\d+)',a).group(2)
        url = 'https://api.vc.bilibili.com/link_draw/v1/doc/detail?doc_id={}'.format(data_id)
        res = requests.get(url,headers=headers).content.decode()
        image_data = json.loads(res)
        try:
            images = image_data['data']['item']['pictures']

            x_2 = 1
            for i in images:
                name = f'壁纸{data_id}_{x_2}'
                res = requests.get(i['img_src'],headers=headers).content
                #小于这个大小的不要
                if len(res) > 1500000:
                    print(f"正在爬取图片 {name}")

                    with open(f'{file_direct}{name}.jpg','wb') as f:
                        f.write(res)
                    x_2 += 1

        except Exception as e:
            print(e)

if __name__ == '__main__':
    get_img(20)
